package top.aylan.org.ctrl.admin;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import top.aylan.org.entity.Category;
import top.aylan.org.entity.User;
import top.aylan.org.service.CategoryService;
import top.aylan.org.tools.Directory;
import top.aylan.org.tools.FileUploader;
import top.aylan.org.tools.Pager;
import top.aylan.org.tools.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created by Administrator on 2018/12/20.
 */
@Controller
@RequestMapping(value = "/admin/category")
public class AdminCategoryController {

    @Resource
    CategoryService categoryService;

    @RequestMapping(value = "/index")
    public String index(ModelMap modelMap, @RequestParam(value = "page",defaultValue = "1")Integer page){

        Pager<Category> pager = new Pager<Category>();
        pager.setCurrentPage(page);
        try {
            pager.setList(categoryService.selectCategoryByPage(pager));
            modelMap.addAttribute("pager",pager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "admin/categories";
    }

    @RequestMapping("/add")
    public String add(@RequestParam(value = "id",defaultValue = "0")Integer id ,ModelMap modelMap){

        try {
            List<Category> list = categoryService.selectParentCategory();
            modelMap.addAttribute("list",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        modelMap.addAttribute("pid",id);
        return "admin/category-add";
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam(value = "id",defaultValue = "0")Integer id ,ModelMap modelMap){

        try {
            Category category = categoryService.findById(id);
            List<Category> list = categoryService.selectParentCategory();
            modelMap.addAttribute("list",list);
            modelMap.addAttribute("category",category);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "admin/category-edit";
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(
            @RequestParam(value = "id",defaultValue = "0") Integer id,
            @RequestParam(value = "title",defaultValue = "") String title,
            @RequestParam(value = "pid",defaultValue = "0")Integer pid,
            MultipartFile file,
            HttpServletRequest request
    ){
        String  status = (String) request.getParameter("status");
        int state = 0;
        if(status != null && status.equals("on")){
            state = 1;
        }
        Category category = new Category();
        category.setId(id);
        category.setTitle(title);
        category.setPid(pid);
        category.setStatus((byte)state);
        FileUploader uploader = new FileUploader();
        Result result = uploader.upload(file, Directory.CATEGORY);
        if(result.getCode() == 1){
            category.setThumb((String) result.getData().get("path"));
        }else if(result.getCode() == 2){
            category.setThumb("");
        }
        try {
            if(id == 0){
                categoryService.save(category);
            }else{
                categoryService.updateById(id,category);
            }
            return "redirect:/admin/category/index";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "admin/category-add";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "id")Integer id){
      if(id != null){
          try {
              categoryService.delete(id);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      return "redirect:/admin/category/index";
    }
}
